package com.yanqu.road.dao.impl.activity.climbtower;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.climbtower.UserClimbTowerData;
import com.yanqu.road.entity.activity.climbtower.UserClimbTowerEvent;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class UserClimbTowerDataDaoImpl extends TempDataObjectDao<UserClimbTowerData> {

    public static String getUserClimbTowerDataSql = "select * from `t_u_climb_tower_data` where `activityId`=? and `userId`=?";
    public UserClimbTowerData getUserClimbTowerData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserClimbTowerDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertUserClimbTowerDataSql = "insert into `t_u_climb_tower_data`(activityId,userId,exp,score,ability,power,lastRecoverPowerTime,drawTimes,cellNum,event,runeNumMap,preHandleList,maxTopCell,lastDefeatedTime,topCellNum) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserClimbTowerData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getExp().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getAbility().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecoverPowerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCellNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEvent())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRuneNumMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getPreHandleList(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMaxTopCell()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastDefeatedTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTopCellNum()));
        if (getDb().executeNoneQuery(insertUserClimbTowerDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserClimbTowerDataSql = "update `t_u_climb_tower_data` set exp=?,score=?,ability=?,power=?,lastRecoverPowerTime=?,drawTimes=?,cellNum=?,event=?,runeNumMap=?,preHandleList=?,maxTopCell=?,lastDefeatedTime=?,topCellNum=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(UserClimbTowerData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getExp().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getAbility().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecoverPowerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCellNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEvent())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRuneNumMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getPreHandleList(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMaxTopCell()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastDefeatedTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTopCellNum()));

        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateUserClimbTowerDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserClimbTowerData parseData(ResultSet rs) throws SQLException {
        UserClimbTowerData userClimbTowerData = new UserClimbTowerData();
        userClimbTowerData.setActivityId(rs.getInt("activityId"));
        userClimbTowerData.setUserId(rs.getLong("userId"));
        userClimbTowerData.setExp(new BigInteger(rs.getString("exp")));
        userClimbTowerData.setScore(rs.getInt("score"));
        userClimbTowerData.setAbility(new BigInteger(rs.getString("ability")));
        userClimbTowerData.setPower(rs.getInt("power"));
        userClimbTowerData.setLastRecoverPowerTime(rs.getLong("lastRecoverPowerTime"));
        userClimbTowerData.setDrawTimes(rs.getInt("drawTimes"));
        userClimbTowerData.setCellNum(rs.getInt("cellNum"));
        userClimbTowerData.setEvent(JSON.parseObject(rs.getString("event"), UserClimbTowerEvent.class));
        userClimbTowerData.setRuneNumMap(JSON.parseObject(rs.getString("runeNumMap"), new TypeReference<Map<Integer, Integer>>() {}));
        userClimbTowerData.setPreHandleList(StringUtils.stringToIntegerList(rs.getString("preHandleList"), ","));
        userClimbTowerData.setMaxTopCell(rs.getInt("maxTopCell"));
        userClimbTowerData.setLastDefeatedTime(rs.getLong("lastDefeatedTime"));
        userClimbTowerData.setTopCellNum(rs.getInt("topCellNum"));
        return userClimbTowerData;
    }
}
